package com.xiaoming.mojbackendcommon.aop;


import com.xiaoming.mojbackendcommon.annotation.Log;
import com.xiaoming.mojbackendcommon.aop.entity.LogInfo;
import com.xiaoming.mojbackendcommon.aop.entity.User;
import com.xiaoming.mojbackendcommon.aop.service.LogService;
import lombok.extern.slf4j.Slf4j;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

import static com.xiaoming.mojbackendcommon.constant.UserConstant.USER_LOGIN_STATE;


@Component
@Aspect
@Slf4j
public class RecordLogAspect {
    @Autowired
    LogService logService;
    /**
     * 切入点
     */
    @Pointcut("@annotation(com.xiaoming.mojbackendcommon.annotation.Log)")
    public void RecordLogAspectCut() {

    }

    @Around("RecordLogAspectCut()")
    public void AutoFill(ProceedingJoinPoint joinPoint) throws Throwable {
        LogInfo logInfo=new LogInfo();

        //获取被增强的类和方法
        Signature signature = joinPoint.getSignature();
        MethodSignature  methodSignature= (MethodSignature) signature;
        //获取方法对象
        Method method = methodSignature.getMethod();
        //获取请求操作名
        if(method!=null)
        {
            Log logAnnotation=method.getAnnotation(Log.class);
            String operationName=logAnnotation.operationName();
            logInfo.setOperationName(operationName);
            System.out.println("请求操作名：" + operationName);
        }
        //获取方法名
        if(method!=null)
        {
            String methodName=method.getName();
            logInfo.setMethodName(methodName);
            System.out.println("请求方法名：" + methodName);
        }
        //获取request对象
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            if (arg instanceof HttpServletRequest) {
                HttpServletRequest request = (HttpServletRequest) arg;
                // 获取IPv4地址
                String ipAddress = request.getRemoteAddr();
                logInfo.setIpAddress(ipAddress);
                System.out.println("IPv4地址：" + ipAddress);

                // 获取请求地址
                String requestURL = request.getRequestURL().toString();
                logInfo.setRequestURL(requestURL);
                System.out.println("请求地址：" + requestURL);

                // 获取请求方式
                String requestMethod = request.getMethod();
                logInfo.setRequestMethod(requestMethod);
                System.out.println("请求方式：" + requestMethod);

                //获取登录对象
                Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
                User currentUser = (User) userObj;
                if(currentUser!=null && currentUser.getUserName()!=null)
                {
                    String userName= currentUser.getUserName();
                    logInfo.setUserName(userName);
                    System.out.println("操作用户名：" + userName);
                }

            }
        }
        logService.save(logInfo);

    }
}
